package com.tianlan.blog.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianlan.blog.dao.SysDictDao;
import com.tianlan.blog.entity.SysDict;
import com.tianlan.blog.service.SysDictService;
import com.tianlan.common.base.BusinessException;

@Service
public class SysDictServiceImpl implements SysDictService {

	@Autowired
	private SysDictDao sysDictDao;
	
	@Override
	public boolean saveDict(SysDict sysDict) {
		SysDict isSysDict = sysDictDao.saveAndFlush(sysDict);
		return isSysDict != null && !"".equals(isSysDict);
	}
	
	@Override
	public boolean updateDict(SysDict sysDict) {
		long isCount = sysDictDao.countByIdAndDelFlag(sysDict.getId(), "0");
		if(isCount == 0){
			throw new BusinessException("字典标识不存在");
		}
		SysDict isSysDict = sysDictDao.save(sysDict);
		sysDictDao.flush();
		return isSysDict != null && !"".equals(isSysDict);
	}
	
	@Override
	public boolean deleteDict(Integer dictId) {
		long isCount = sysDictDao.countByIdAndDelFlag(dictId, "0");
		if(isCount == 0){
			throw new BusinessException("字典标识不存在");
		}
		int isRemove = sysDictDao.removeDict(dictId);
		return isRemove != 0;
	}
	
	
	@Override
	public List<SysDict> queryDictTreeByPanentId(Integer parentId) {
        if (null == parentId) {
            parentId = 0;
        }
        List<SysDict> dictList =  sysDictDao.queryDictList(parentId);
        List<SysDict> dicts = new ArrayList<SysDict>();
        Map<Integer, SysDict> dictMap = new HashMap<Integer, SysDict>();
        // 组装子父级目录
        for (SysDict dict : dictList) {
            dictMap.put(dict.getId(), dict);
        }
        for (SysDict dict : dictList) {
            Integer treePId = dict.getParentId();
            SysDict pTreev = dictMap.get(treePId);
            if (null != pTreev && !dict.equals(pTreev)) {
                List<SysDict> nodes = pTreev.getChildren();
                if (null == nodes) {
                    nodes = new ArrayList<SysDict>();
                    pTreev.setChildren(nodes);
                }
                nodes.add(dict);
            } else {
                dicts.add(dict);
            }
        }
        return dicts;
	}
	
//	@Override
//	public List<DictInfo> queryDictTreeByPanentId(Integer parentId) {
//        if (null == parentId) {
//            parentId = 0;
//        }
//        List<DictInfo> dictList =  sysDictDao.queryDictList(parentId);
//        List<DictInfo> dicts = new ArrayList<DictInfo>();
//        Map<Integer, DictInfo> dictMap = new HashMap<Integer, DictInfo>();
//        // 组装子父级目录
//        for (DictInfo dict : dictList) {
//            dictMap.put(dict.getId(), dict);
//        }
//        for (DictInfo dict : dictList) {
//            Integer treePId = dict.getParentId();
//            DictInfo pTreev = dictMap.get(treePId);
//            if (null != pTreev && !dict.equals(pTreev)) {
//                List<DictInfo> nodes = pTreev.getChildren();
//                if (null == nodes) {
//                    nodes = new ArrayList<DictInfo>();
//                    pTreev.setChildren(nodes);
//                }
//                nodes.add(dict);
//            } else {
//                dicts.add(dict);
//            }
//        }
//        return dicts;
//	}

}
